﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Transactions;
using DAL;
using Common;

namespace BLL
{
    /// <summary>
    /// 
    /// </summary>
    public class SysLogBLL : BaseBLL
    {
        /// <summary>
        /// 
        /// </summary>
        SysLogRepository repository = null;
        public SysLogBLL()
        {
            repository = new SysLogRepository();
        }
        public FlexigridObject FlexigridList(string id, FlexigridParam param)
        {
            int page = param.Page.GetInt();
            int rp = param.RP.GetInt();

            using (SysEntities db = new SysEntities())
            {
                IQueryable<SysLog> queryData = repository.DaoChuData(db, param);
                int count = queryData.Count();
                if (count > 0)
                {
                    queryData = queryData.Skip((page - 1) * rp).Take(rp);
                     
                }
                FlexigridObject flexigridObject = new FlexigridObject()
                {
                    FlexigridPara = param,
                    total = (count > 0) ? count : 0,
                    Query = queryData
                };
                return flexigridObject;
            }
        }
        /// <summary>
        /// 在查询中输入字符串，自动提示的功能
        /// </summary>
        /// <param name="id">需要查询的数据库字段的名称</param>
        /// <param name="term">输入的字符串</param>
        /// <returns></returns>  
        public string SearchAutoComplete(string id, string term)
        {
            FlexigridParam param = new FlexigridParam();
            param.Query = id + "&" + term + "^";
            param.SortName = id;

            List<string> listString = new List<string>();
            using (SysEntities db = new SysEntities())
            {
                var queryData = repository.DaoChuData(db, param);
                if (queryData != null)
                {
                    queryData = queryData.Take(5).Distinct();
                    foreach (var item in queryData)
                    {
                        listString.Add(FlexigridRow.GetPropertyList(item, new string[] { id }).FirstOrDefault());
                    }
                }
            }
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append("[");
            int i = 0;
            foreach (var item in listString)
            {
                if (string.IsNullOrEmpty(item))
                {
                    continue;
                }
                if (i == 0)
                {
                    i++;
                    stringBuilder.Append(@"{@label@:@" + item + "@}");
                }
                else
                {
                    stringBuilder.Append(",");
                    stringBuilder.Append(@"{@label@:@" + item + "@}");
                }
            }
            stringBuilder.Append("]");
            return stringBuilder.ToString().Replace('@', '"') ;
        }
        public bool CreateCollection(ref ValidationErrors validationErrors, IQueryable<SysLog> entitys)
        {
            if (entitys != null)
            {
                int count = entitys.Count();
                if (count == 1)
                {
                    return this.Create(ref validationErrors, entitys.FirstOrDefault());
                }
                else if (count > 1)
                {
                    using (TransactionScope transactionScope = new TransactionScope())
                    {
                        using (SysEntities db = new SysEntities())
                        {
                            repository.Create(db, entitys);
                            if (count == repository.Save(db))
                            {
                                transactionScope.Complete();
                                return true;
                            }
                            else
                            {
                                Transaction.Current.Rollback();
                            }
                        }
                    }
                }
            }
            return false;
        }
        public bool Create(ref ValidationErrors validationErrors, SysLog entity)
        {
            return repository.Create(entity) == 1;
        }

        public bool DeleteCollection(ref ValidationErrors validationErrors, string[] deleteCollection)
        {
            if (deleteCollection != null)
            {
                if (deleteCollection.Length == 1)
                {
                    return this.Delete(ref validationErrors, deleteCollection[0]);
                }
                else if (deleteCollection.Length > 1)
                {
                    using (TransactionScope transactionScope = new TransactionScope())
                    {
                        using (SysEntities db = new SysEntities())
                        {
                            repository.Delete(db, deleteCollection);
                            if (deleteCollection.Length == repository.Save(db))
                            {
                                transactionScope.Complete();
                                return true;
                            }
                            else
                            {
                                Transaction.Current.Rollback();
                            }
                        }
                    }
                }
            }
            return false;
        }
        public bool Delete(ref ValidationErrors validationErrors, string id)
        {
            return repository.Delete(id) == 1;
        }
        public bool EditCollection(ref ValidationErrors validationErrors, IQueryable<SysLog> entitys)
        {
            if (entitys != null)
            {
                int count = entitys.Count();
                if (count == 1)
                {
                    return this.Edit(ref validationErrors, entitys.FirstOrDefault());
                }
                else if (count > 1)
                {
                    using (TransactionScope transactionScope = new TransactionScope())
                    {
                        using (SysEntities db = new SysEntities())
                        {
                            repository.Edit(db, entitys);
                            if (count == repository.Save(db))
                            {
                                transactionScope.Complete();
                                return true;
                            }
                            else
                            {
                                Transaction.Current.Rollback();
                            }
                        }
                    }
                }
            }
            return false;
        }
 
        public bool Edit(ref ValidationErrors validationErrors, SysLog entity)
        {
            using (SysEntities db = new SysEntities())
            {
                repository.Edit(db, entity);
                return repository.Save(db) == 1;
            }
        }
      
        public List<SysLog> GetAll()
        {
            SysEntities db = new SysEntities();            
            return repository.GetAll(db).ToList();          
        }   
        public SysLog GetById(string id)
        {
            SysEntities db = new SysEntities();            
            return repository.GetById(db, id);           
        }
    }
}

